<template>
  <div class="container"><el-dialog
    :title=" ruleForm.id ? '编辑文章':'新增文章'"
    :visible.sync="showDialog"
    width="60%"
    @close="close"
  >
    <el-form
      ref="ruleForm"
      :model="ruleForm"
      :rules="rules"
      label-width="100px"
    >
      <el-form-item label="文章标题" prop="title">
        <el-input v-model="ruleForm.title" />
      </el-form-item>
      <el-form-item label="文章内容" prop="articleBody">
        <quill-editor v-model="ruleForm.articleBody" :options="editorOptions" class="height" />
      </el-form-item>
      <el-form-item label="视频地址" style="margin-top: 65px;">
        <el-input v-model="ruleForm.videoURL" />
      </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="close">取 消</el-button>
      <el-button type="primary" @click="addArticle">确 定</el-button>
    </span>
  </el-dialog>
  </div>
</template>

<script>
import { add, update } from '@/api/hmmm/articles'
import 'quill/dist/quill.core.css'
import 'quill/dist/quill.snow.css'
import 'quill/dist/quill.bubble.css'
import { quillEditor } from 'vue-quill-editor'
export default {
  name: 'ArticlesAdd',
  components: { quillEditor },
  props: {

  },
  data() {
    return {
      showDialog: false,
      ruleForm: {
        title: '',
        articleBody: '',
        videoURL: ''

      },
      rules: {
        title: [
          { required: true, message: '请输入文章的标题' }
        ],
        articleBody: [
          {
            required: true
          }
        ]

      },
      editorOptions: {
        placeholder: '',
        modules: {
          toolbar: [
            ['bold', 'italic', 'underline', 'strike'],
            [{ list: 'ordered' }, { list: 'bullet' }],
            ['blockquote'],
            ['code-block', 'image', 'link']
          ]
        }
      }
    }
  },
  methods: {
    close() {
      this.showDialog = false
      this.$refs.ruleForm.resetFields()
      this.ruleForm = {
        title: '',
        articleBody: '',
        videoURL: ''
      }
    },
    async addArticle() {
      try {
        await this.$refs.ruleForm.validate()
      } catch {
        return
      }
      if (this.ruleForm.id) {
        await update(this.ruleForm)
        this.$message.success('编辑成功')
      } else {
        await add(this.ruleForm)

        this.$message.success('添加成功')
      }
      // console.log(res)

      this.showDialog = false
      this.$emit('update')
    },
    open({ articleBody, title, videoURL, id }) {
      this.showDialog = true

      this.ruleForm = { articleBody, title, videoURL, id }
    }
  }
}
</script>

<style scoped lang='scss'>
.height {
  height: 400px;
  margin-bottom: 10px;
}

</style>
